﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace Fares
{
    public partial class OptiuniRapoarteStocuriFrm : FrmCard
    {
        public int LunaCurenta = 0;
        public int AnulCurent = 0;
        private String SerieNumerica = string.Empty;
        private Int16 tipRaport = 0;
        private Boolean ParteneriProprii = true;

        public OptiuniRapoarteStocuriFrm(ModuleClass mc,Int16 _tiprap):
            base(mc)
        {
            tipRaport = _tiprap;
            InitializeComponent();
            workConnString = mc.workConnectionString;
            switch(tipRaport)
            {
                case 1:
                    Text = "Optiuni rapoarte - Stocuri";
                    break;
                 default:
                    Text = "Optiuni rapoarte";
                    break;
            }
            DeLaDTP.Format = DateTimePickerFormat.Custom;
            DeLaDTP.CustomFormat = "dd.MM.yyyy";



            tipOperatiunePlataBindingSource.Add(new TipOperatiunePlata(">", "0"));
            tipOperatiunePlataBindingSource.Add(new TipOperatiunePlata(">=", "1"));
            tipOperatiunePlataBindingSource.Add(new TipOperatiunePlata("=", "2"));
            tipOperatiunePlataBindingSource.Add(new TipOperatiunePlata("<=", "3"));
            tipOperatiunePlataBindingSource.Add(new TipOperatiunePlata("<", "4"));
            tipOperatiunePlataBindingSource.Add(new TipOperatiunePlata("<>", "5"));
            tipOperatiunePlataBindingSource.Position = 0;
        }

        private void OptiuniRapoarteFrm_Load(object sender, EventArgs e)
        {
            
            SqlConnection con = DeschideConexiune(workConnString);

            #region Determinare luna,an curent
            Object ret = GetSetupValue("FaresSetup", "IDSerie", ModuleInfo.CodUnitate, con);
            String val = String.Empty;
            if (ret != null && ret != DBNull.Value)
                val = ret.ToString();
            else
            {
                while (ret == null)
                {
                    MessageBox.Show("Modulul Parteneri nu a fost configurat. \nVa rugam sa-l configurati!",
                            "Eroare",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Hand);
                    FaresSetupFrm frm = new FaresSetupFrm(this.ModuleInfo);
                    frm.ShowDialog();
                    ret = GetSetupValue("FaresSetup", "IDSerie", ModuleInfo.CodUnitate, con);
                }
            }
            SerieNumerica = val;
            Object retObj = GetSetupValue("FaresSetup", "LunaCurenta", ModuleInfo.CodUnitate, con);
            if (retObj != null && retObj != DBNull.Value)
                LunaCurenta = Convert.ToInt32(retObj);
            retObj = GetSetupValue("FaresSetup", "AnCurent", ModuleInfo.CodUnitate, con);
            if (retObj != null && retObj != DBNull.Value)
                AnulCurent = Convert.ToInt32(retObj);

            retObj = GetSetupValue("FaresSetup", "ParteneriProprii", ModuleInfo.CodUnitate, con);
            if (retObj != null && retObj != DBNull.Value)
                ParteneriProprii = Convert.ToBoolean(retObj);

            #endregion
            DateTime inceputLuna = new DateTime(AnulCurent, LunaCurenta, 1);
            DateTime sfarsitLuna = inceputLuna.AddMonths(1).AddDays(-1);
            DeLaDTP.Value = inceputLuna;

            switch (tipRaport)
            {
                case 1:
                   break;
                default:
                    break;
            }
            this.tipCategoriiArticoleTableAdapter.Connection = con;
            this.tipCategoriiArticoleTableAdapter.Fill(this.esrpDataSetListeStocuri.TipCategoriiArticole);
            this.articoleTableAdapter.Connection = con;
            this.articoleTableAdapter.Fill(this.esrpDataSetListeStocuri.Articole, ModuleInfo.CodUnitate);
            this.fares_ParteneriCUITableAdapter.Connection = con;
            if(ParteneriProprii)
                this.fares_ParteneriCUITableAdapter.FillByUnitate(this.esrpDataSetListeStocuri.fares_ParteneriCUI, ModuleInfo.CodUnitate);
            else
                this.fares_ParteneriCUITableAdapter.Fill(this.esrpDataSetListeStocuri.fares_ParteneriCUI);
            

            AddButton("Genereaza[F9]", global::ERPStudio.Properties.Resources._001_07, Genereaza, ToolStripItemAlignment.Right, false);

           

            HandleSalvare += Genereaza;
            InchideConexiune(con);
        }

        private void Genereaza(object sender, EventArgs e)
        {
            String filtruArt = string.Empty;
            String filtruPart = string.Empty;
            String filtruCat = string.Empty;
            String NumeRaport = "Documente.rdlc";
            switch(tipRaport)
            {
                case 1:
                    NumeRaport = "CentralizatorStocuri.rdlc";
                    break;
                default:
                    return;

            }
            if (partenerRB.Checked)
            {
                if (partenerCB.SelectedValue!=null)
                    filtruPart = partenerCB.SelectedValue.ToString();
            }
            else
            if (parteneriRB.Checked)
            {
                for (Int16 i = 0; i < articoleSelectateBindingSource.Count; i++)
                {
                    if (filtruPart != String.Empty)
                        filtruPart += ",";
                    filtruPart += ((FurnizoriArticoleSelectate)articoleSelectateBindingSource.List[i]).Tag.ToString();
                }
            }

            if (articoleGB.Visible && articolRB.Checked)
            {
                filtruArt = articolCB.SelectedValue.ToString();
            }
            if (articoleGB.Visible &&  articoleRB.Checked)
            {
                for (Int16 i = 0; i < articoleSelectateBindingSource.Count; i++)
                {
                    if (filtruArt != String.Empty)
                        filtruArt += ",";
                    filtruArt += ((FurnizoriArticoleSelectate)articoleSelectateBindingSource.List[i]).Tag.ToString();
                }
            }
            if (categoriiGB.Visible && categorieRB.Checked)
            {
                filtruCat = categorieCB.SelectedValue.ToString();
            }
            if (categoriiGB.Visible && categoriiRB.Checked)
            {
                for (Int16 i = 0; i < categoriiSelectateBindingSource.Count; i++)
                {
                    if (filtruCat != String.Empty)
                        filtruCat += ",";
                    filtruCat += ((FurnizoriArticoleSelectate)categoriiSelectateBindingSource.List[i]).Tag.ToString();
                }
            }

            PreviewRaportFrm frm = new PreviewRaportFrm(NumeRaport, null, ModuleInfo);
            
            switch (tipRaport)
            {
                case 1:
                    try
                    {
                        esrpDataSetListeStocuri.EnforceConstraints = false;
                        this.fares_FiltreazaListaStocuriTableAdapter.Connection = DeschideConexiune(workConnString);
                        this.fares_FiltreazaListaStocuriTableAdapter.Fill(this.esrpDataSetListeStocuri.fares_FiltreazaListaStocuri,
                            filtruArt, filtruCat, DeLaDTP.Value,StocZeroCB.Checked,Convert.ToInt16(OperatieCB.SelectedValue), ValoareStocNUD.Value, ModuleInfo.CodUnitate);
                        this.balconSetupTableAdapter.Connection = this.fares_FiltreazaListaStocuriTableAdapter.Connection;
                        this.balconSetupTableAdapter.Fill(this.esrpDataSetListeStocuri.BalconSetup,ModuleInfo.CodUnitate);
                        InchideConexiune(this.fares_FiltreazaListaStocuriTableAdapter.Connection);
                    }
                    catch (System.Exception ex)
                    {
                        TrateazaExceptie(ex);
                        return;
                    }                    
                break;
                default:
                    break;
            }        
            switch (tipRaport)
            {
                case 1:
                    frm.AddDataSource("fares_FiltreazaListeStocuri", fares_FiltreazaListaStocuriBindingSource);
                    break;
                default:
                    break;
            
            }
            
            frm.AddDataSource("BalconSetup", balconSetupBindingSource);
            switch (tipRaport)
            {
                case 1:
                    frm.AddParameter("Linia1", "Situatia stocurilor", true);                   
                    break;
                default:
                    return;

            }
            
            frm.AddParameter("Linia2", "La data " + DeLaDTP.Value.Day.ToString() + "." + DeLaDTP.Value.Month.ToString() + "." + DeLaDTP.Value.Year.ToString() , true);

            
            frm.MdiParent = MdiParent;
            
            frm.Show();
        
        }

        private void addPartenerB_Click(object sender, EventArgs e)
        {
            if (partenerCB.SelectedValue == null)
                return;
            furnizoriSelectateBindingSource.Add(new FurnizoriArticoleSelectate(partenerCB.Text,
                Convert.ToInt64(partenerCB.SelectedValue)));
            parteneriRB.Checked = true;
        }

        private void delPart_Click(object sender, EventArgs e)
        {
            if (furnizoriSelectateBindingSource.Count>0)
                furnizoriSelectateBindingSource.RemoveCurrent();
        }

        private void addArt_Click(object sender, EventArgs e)
        {
            articoleSelectateBindingSource.Add(new FurnizoriArticoleSelectate(articolCB.Text,
                Convert.ToInt16(articolCB.SelectedValue)));
            articoleRB.Checked = true;
        }

        private void delArt_Click(object sender, EventArgs e)
        {
            articoleSelectateBindingSource.RemoveCurrent();
        }

        private void tipuriCLB_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == e.CurrentValue)
                return;
            CheckedListBox clb = sender as CheckedListBox;
            _itemInfo ci = clb.Items[e.Index] as _itemInfo;

        }

        private void addCat_Click(object sender, EventArgs e)
        {
            categoriiSelectateBindingSource.Add(new FurnizoriArticoleSelectate(categorieCB.Text,
                Convert.ToInt16(categorieCB.SelectedValue)));
            categoriiRB.Checked = true;
        }

        private void delCat_Click(object sender, EventArgs e)
        {
            if (categoriiSelectateBindingSource.Count > 0)
                categoriiSelectateBindingSource.RemoveCurrent();
        }

               
    }
    
}
